package com.cn.web.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cn.error.ErrorConstantUtil;
import com.cn.exception.MyException;
import com.cn.group.GroupDO;
import com.cn.page.PageVo;
import com.cn.user.UserDO;
import com.cn.util.AuthUserContext;
import com.cn.util.DateUtil;
import com.cn.util.SnowflakeIdWorkerUtil;
import com.cn.web.user.mapper.GroupMapper;
import com.cn.web.user.service.GroupService;
import com.cn.web.user.service.UserGroupService;
import com.cn.web.user.vo.GroupSearch;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * description: GroupService <br>
 * date: 23.5.8 13:33 <br>
 * author: cn_yaojin <br>
 * version: 1.0 <br>
 */
@Service
@Slf4j
public class GroupServiceImpl implements GroupService {

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private UserGroupService userGroupService;

    @Override
    public void edit(GroupDO group) {
        QueryWrapper<GroupDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_name", group.getGroupName());
        if (group.getId() != null) {
            queryWrapper.ne("id", group.getId());
        }
        long count = this.groupMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new MyException(ErrorConstantUtil.GROUP_EXSIT_ERROR);
        }
        UserDO user = AuthUserContext.get();
        String time = DateUtil.getTime("yyyy-MM-dd HH:mm:ss");
        if (group.getId() == null || group.getId() == 0) {
            group.setId(SnowflakeIdWorkerUtil.getId());
            group.setAddId(user.getId().toString()).setAddName(user.getUserName()).setAddTime(time);
            this.groupMapper.insert(group);
        } else {
            group.setUpdateId(user.getId().toString()).setUpdateName(user.getUserName()).setUpdateTime(time);
            this.groupMapper.updateById(group);
        }
    }

    @Override
    public void del(String id) {
        this.userGroupService.checkExsitByUserId(id);
        this.groupMapper.deleteById(id);
    }

    @Override
    public List<GroupDO> list(GroupSearch search) {
        QueryWrapper<GroupDO> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            if (StringUtils.isNotEmpty(search.getGroupName())) {
                queryWrapper.like("group_name", search.getGroupName());
            }
        }
        queryWrapper.orderByDesc("add_time");
        List<GroupDO> list = this.groupMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public PageInfo<GroupDO> page(PageVo<GroupSearch> pageVo) {
        return PageHelper.startPage(pageVo.getPageNum(), pageVo.getPageSize()).doSelectPageInfo(() -> {
            list(pageVo.getParam());
        });
    }

}
